//////////////////////////////////////////////////////////////////////////
//
// This file is a part of XORT project.
//
// PROJECT:         Xort compiler execution subsystem
// FILE:            XortMachine.h - CPU + stack state for JIT code generator
// AUTHOR:          Vladimir Gumenuk
// DATE:            27-Oct-2014
// MODIFICATIONS:
//
//
//     Copyright(c) 2014, Vladimir Gumenuk
//     All rights reserved.
//
//     Redistribution and use in source and binary forms, with or without modification, 
//     are permitted provided that the following conditions are met :
//
//     1. Redistributions of source code must retain the above copyright notice, 
//        this list of conditions and the following disclaimer.
//     2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 
//        the following disclaimer in the documentation and / or other materials provided with the distribution.
//     3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse 
//        or promote products derived from this software without specific prior written permission.
//
//     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
//     THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//     OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
//     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
//     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#pragma once

#include "xgendefs.h"
#include "Registers.h"

class XortMachine
{
public:
    XortMachine();
    virtual ~XortMachine();

    // this stuff should be called before every xort proc processing
    void BuildArgumentsLayout( Symbol *pFunc );  // fill registers and stack with arguments before function call

    void ReserveLocalStorage( int iNumLocals );  // reserves local on stack for specified number of variables
    void RemoveLocalStorage( int iNumLocals );

    int  getStackBase() const;
    int  getStackTop() const;
    int  getLocalStorageEnd() const;

    void Push( const XortElement& val ); // push some value to hardvare stack
    XortElement Pop();                   // pop some value from hardware stack

    bool testCanAccessStk( int iIdx );

    XortElement getRegValue( Register64 rg );
    XortElement getFromStack( int iIdx );

    VarStorage  findStorage( const XortElement& val ); // return empty slot in cpu registers or on hardware stack
    void        clearStorage( const VarStorage& st );

    VarStorage  findLocalStorage( const XortElement& val ); // return empty slot from local variables
    void        clearLocalStorage( VarStorage st );

    XortElement getFromStorage( const VarStorage& st );

private:
    XRegsisters         m_Registers;    // store the information about register state during code generation
    XCELLSARRAY         m_Stack;        // used to emulate hardware stack during code generation
    dword               m_dwStackBase;  // base of the locals area
    dword               m_dwLocalStorageStartIdx; // index of the start of the local storage
    dword               m_dwNumLocals;  // count of local storage vars
};
